package hip.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

import org.apache.commons.lang3.StringUtils;

public class Base64Utils {

    /**
     * 编码成base64编码字符串
     *
     * @param str     需要进行编码的字符串
     * @param charset 编码字符集
     * @return base64编码后的字符串
     */
    public static String base64Escape(String str, String charset) {
        if (StringUtils.isBlank(str)) {
            return StringUtils.EMPTY;
        }
        try {
            return Base64.getEncoder().encodeToString(str.getBytes(charset));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解码base64编码字符串
     *
     * @param str     base64编码字符串
     * @param charset 解码字符集
     * @return base64解码后的字符串
     */
    public static String base64UnEscape(String str, String charset) throws UnsupportedEncodingException {
        if (StringUtils.isBlank(str)) {
            return StringUtils.EMPTY;
        }
        byte[] bytes = Base64.getDecoder().decode(str);
        ;


        return new String(bytes, charset);
    }

    /**
     * 编码
     *
     * @return String
     */
    public static String encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String encodeForString(String bstr) {
        return Base64.getEncoder().encodeToString(bstr.getBytes());
    }

    /**
     * 解码
     *
     * @param str
     * @return string
     */
    public static byte[] decode(String str) {
        byte[] bt = null;
        try {

            bt = Base64.getDecoder().decode(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bt;
    }

    public static String decodeToString(String str) {
        try {
            return new String(decode(str), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 将base64编码字符串转换为图片,一般插件返回的base64编码的字符串都是有一个前缀的,"data:image/jpeg;base64," 解码之前这个得去掉
     *
     * @param imgStr  base64编码字符串
     * @param imgPath 图片路径-具体到文件
     * @return 是否转换成功
     */
    public static boolean generateImage(String imgStr, String imgPath) {
        if (imgStr == null) {
            return false;
        }

//		BASE64Decoder decoder = new BASE64Decoder();
        Base64.Decoder decoder = Base64.getDecoder();

        try {
            // 解密
            byte[] b = decoder.decode(imgStr);
            for (int i = 0; i < b.length; i++) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgPath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 根据图片地址转换为base64编码字符串
     *
     * @param imgPath 图片地址
     * @return 图片的base64编码字符串
     */
    public static String getImageStr(String imgPath) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgPath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
//	    BASE64Encoder encoder = new BASE64Encoder();
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(data);
    }

}
